package role

/*
 * role manager
 */
import (
	"sync"
)

type roleMgr struct {
	roleMap sync.Map
}

// create role.
func (rm *roleMgr) CreateRole() *Role {
	return NewRole()
}

// Register register uid to *Role.
func (rm *roleMgr) Register(role *Role) {
	rm.roleMap.Store(role.GetUid(), role)
}

// Find find *Role from uid.
func (rm *roleMgr) Find(uid uint64) *Role {
	if v, ok := rm.roleMap.Load(uid); ok {
		return v.(*Role)
	} else {
		return nil
	}
}

// Remove remove *Role.
func (rm *roleMgr) Remove(r *Role) {
	rm.roleMap.Delete(r.GetUid())
}

// Release release roleMgr.
func (rm *roleMgr) Release() {
	rm.roleMap.Range(func(key, value interface{}) bool {
		if r, ok := value.(*Role); ok {
			r.Release()
		}
		return true
	})
}

// global RoleMgr object.
var RoleMgrObj roleMgr
